package cn.jhc.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.jhc.bean.Student;
import cn.jhc.bean.Teacher;
import cn.jhc.dao.TeacherDao;
import cn.jhc.util.HibernateUtil;

public class TeacherDaoImpl implements TeacherDao {
	
	private static Logger LOG = Logger.getLogger(TeacherDaoImpl.class.getName());

	@Override
	public void save(Object o) {
		if (o instanceof Teacher) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("save失败，对象不是" + Teacher.class.getName() + "的对象");
	}

	@Override
	public void update(Object o) {
		if (o instanceof Teacher) {
			System.out.println(((Teacher)o).getTid());
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("update失败，对象不是" + Teacher.class.getName() + "的对象");
	}

	@Override
	public void delete(Object o) {
		if (o instanceof Teacher) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("delete失败，对象不是" + Teacher.class.getName() + "的对象");
	}

	@Override
	public boolean verify(String username, String password) {
		String hql = "select count(t.teacherno) from Teacher t where t.teacherno = ? and t.tpassword = ?";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setString(0, username);
		query.setString(1, password);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		LOG.info("count " + count);
		return count == 1;
	}

	@Override
	public boolean verify(Object o) {
		if (o instanceof Teacher) {
			Teacher teacher = (Teacher)o;
			return verify(teacher.getTeacherno().toString(), teacher.getTpassword());
		}
		LOG.info("verify失败，对象不是" + Teacher.class.getName() + "的对象");
		return false;
	}

	@Override
	public boolean isThereUser(String username) {
		String hql = "select count(t.teacherno) from Teacher t where t.teacherno = ?";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setString(0, username);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		return count != 0;
	}

	@Override
	public Teacher getTeacher(String username) {
		List<Teacher> teachers = null;
		String hql = "from Teacher t where t.teacherno = ?";
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query =session.createQuery(hql);
		query.setString(0, username);
		teachers = query.list();
		session.getTransaction().commit();
		if (teachers != null && teachers.size() >= 1) {
			return teachers.get(0);
		}
		return null;
	}

	@Override
	public Object get(int id) {
		Teacher teacher = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		teacher = session.get(Teacher.class, id);
		session.getTransaction().commit();
		return teacher;
	}

	@Override
	public List list() {
		List teachers = null;
		String hql = "from Teacher";
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		teachers = query.list();
		session.getTransaction().commit();
		return teachers;
	}

	@Override
	public List list(int start, int count) {
		List teachers = null;
		String hql = "from Teacher";
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(start + count);
		teachers = query.list();
		session.getTransaction().commit();
		return teachers;
	}

	@Override
	public long getCount() {
		String hql = "select count(t.teacherno) from Teacher t";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		return count;
	}

}
